//
//  ViewController.m
//  StringRange
//
//  Created by DengTianran on 16/3/7.
//  Copyright © 2016年 DengTianran. All rights reserved.
//
#import "ExciseViewController.h"
#import "ViewController.h"


//@interface NSArray (Log)
//
//@end
//
//@interface NSDictionary (Log)
//
//@end
//@implementation NSArray (Log)
//
//- (NSString *)descriptionWithLocale:(id)locale
//{
//    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
//    
//    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        [strM appendFormat:@"\t%@,\n", obj];
//    }];
//    
//    [strM appendString:@")"];
//    
//    return strM;
//}
//
//@end
//
//@implementation NSDictionary (Log)
//
//- (NSString *)descriptionWithLocale:(id)locale
//{
//    NSMutableString *strM = [NSMutableString stringWithString:@"{\n"];
//    
//    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
//        [strM appendFormat:@"\t%@ = %@;\n", key, obj];
//    }];
//    
//    [strM appendString:@"}\n"];
//    
//    return strM;
//}
//
//@end

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *rangeLabel;
@property (nonatomic, strong)NSMutableArray *array1;
@property (nonatomic, strong)NSMutableArray *array2;

@end

@implementation ViewController
- (IBAction)clickButton:(UIButton *)sender
{
    ExciseViewController *exciseVC = [ExciseViewController new];
    [self.navigationController pushViewController:exciseVC animated:YES];
}
- (IBAction)changeValue:(UIButton *)sender
{
    [_array2 removeAllObjects];
    NSLog(@"array2 =%@", self.array2);
    NSLog(@"array1 = %@", self.array1);
    NSLog(@"%p", self.array1);
        NSLog(@"%p", self.array2);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array1 = [@[@"qwq",@"wwq", @"1221", @"76665"]mutableCopy];
    self.array2 = [NSMutableArray arrayWithArray:_array1];
    NSLog(@"%p", self.array1);
    NSLog(@"%p", self.array2);
    self.rangeLabel.textColor = [UIColor greenColor];
    [self.rangeLabel adjustsFontSizeToFitWidth];
    NSString *string1 = @"我是一个好人，是吗？";
    NSString *string2 = @"一个好人，";
    NSRange range = [string1 rangeOfString:string2];
    NSLog(@"%@", NSStringFromRange(range));
    if (range.location != NSNotFound)
    {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string1];
        
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        
        
        self.rangeLabel.attributedText = attributedString;
        
    }else
    {
        self.rangeLabel.text = string1;

    }
    
//    NSString * idenCard = @"12345678910111s131";
//    BOOL isCard =   [self validateIdentityCard:idenCard];
//    NSArray *array = @[@"额外任务二", @"额为任务", @"服务服务范围", @"我去问问去", @"稳定"];
//    NSDictionary *dictionary = @{@"mn" : @[@"丰富的郭德纲" , @"草当时的方法", @"不敢表白", @"规范"] , @"cs" : @"发给", @"为期" : @"我去额收到收到", @"wqw" : @"去问问"};
//    
//    NSDictionary *dic = @{@"qwqw" : @[@{@"ffg" : @"jyjky", }, @{@"gg" : @"asdas"}, @"gbgg"], @"cxzc" : @"ssads" };
    NSString *word = @"厦门";
    NSTimeInterval startTime=[[NSDate date] timeIntervalSince1970];
    [self returnFirstWordWithString:word];
    NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval totalTime= endTime-startTime;
    NSLog(@"%f", totalTime);
    
    
    //排序
//    [self sort];
    


    
}


/**
 *  排序
 */
- (void)sort
{
    
    NSMutableArray * okArr = [@[]mutableCopy];
    for (int i = 0; i < 10; i++) {
        [NSThread sleepForTimeInterval:1];
        NSDate *date = [NSDate date];
        [okArr addObject:date];
    }
    NSArray *newOrderArr = [okArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSComparisonResult result = [obj1 compare:obj2];
        switch(result)
        {
            case NSOrderedAscending:
                return NSOrderedDescending;
            case NSOrderedDescending:
                return NSOrderedAscending;
            case NSOrderedSame:
                return NSOrderedSame;
            default:
                return NSOrderedSame;
        }
    }];
    
    NSLog(@"%@", newOrderArr);
}

/**
 *  获取首字母
 *
 *  @param string <#string description#>
 *
 *  @return <#return value description#>
 */
- (NSString *)returnFirstWordWithString:(NSString *)string
{
    if ([string canBeConvertedToEncoding:NSASCIIStringEncoding])
    {
        return [[string substringToIndex:1] uppercaseString];
        
    }else
    {
        //转成了可变字符串
        NSMutableString *str = [NSMutableString stringWithString:string];
        //先转换为带声调的拼音
        CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
        //再转换为不带声调的拼音
        CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
        //转化为大写拼音
        NSString *pinYin = [str capitalizedString];
        //获取并返回首字母
        return [pinYin substringToIndex:1];
        
    }
   

}


/**
 *  身份证得正则表达式
 *
 *  @param identityCard
 *
 *  @return
 */
-(BOOL)validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
